package com.weicx.wuzhiops.util;

import com.weicx.wuzhiops.constant.event.EventKeys;
import com.weicx.wuzhiops.model.dto.UserDTO;

import java.util.HashMap;

/**
 *  操作日志参数
 * @author:
 * @date: 2022-12-07 10:29:18
 * @since: 1.0
 */
public class EventParamsHolder {

    private static final ThreadLocal<HashMap<String, Object>> PARAMS = ThreadLocal.withInitial(HashMap::new);


    public static HashMap<String, Object> get() {
        return PARAMS.get();
    }

    public static void set(HashMap<String, Object> params) {
        PARAMS.set(params);
    }


    public static void remove() {
        PARAMS.remove();
    }


    public static void addParam(String key, Object value) {
        PARAMS.get().put(key, value);
    }


    /**
     * 设置参数
     *
     * @param value value
     */
    public static void addParams(Object value) {
        if (value == null) {
            return;
        }
        PARAMS.get().putAll(BeanMapUtil.bean2Map(value));
    }

    /**
     * 设置默认参数
     */
    public static void setDefaultEventParams() {
        UserDTO user = UserHolder.get();
        if (user != null) {
            EventParamsHolder.addParam(EventKeys.INNER_USER_ID, user.getId());
            EventParamsHolder.addParam(EventKeys.INNER_USER_NAME, user.getUsername());
        }
    }





}
